<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CSVReader Class
*
* $Id: csvreader.php 147 2007-07-09 23:12:45Z Pierre-Jean $
*
* Allows to retrieve a CSV file content as a two dimensional array.
* The first text line shall contains the column names.
*
* @author      Pierre-Jean Turpeau
* @link        http://www.codeigniter.com/wiki/CSVReader

  EXAMPLE:
  
  CSV File body:
  
	Id,Name,Category,Price
	1,iPhone,Mobile,300,
	2,iMac,Desktop,529,
	3,MacBook,Mobile,2000,
	4,iTouch,Gadgets,157,
	5,Wii,Gaming,1250,
  
  Controller function body:
  
    $this->load->library('csvreader');

    $filePath = './csv/products.csv';

    $data['csvData'] = $this->csvreader->parse_file($filePath);

    $this->load->view('csv_view', $data);
		   
		   
  View file body:
  
    <table cellpadding="0" cellspacing="0">
        <thead>
        <th>
                <td>PRODUCT ID</td>
                <td>PRODUCT NAME</td>
                <td>CATEGORY</td>
                <td>PRICE</td>
        </th>
        </thead>

        <tbody>
                <?php foreach($csvData as $field){?>
                    <tr>
                        <td><?=$field['id']?></td>
                       <td><?=$field['name']?></td>
                        <td><?=$field['category']?></td>
                        <td><?=$field['price']?></td>
                    </tr>
                <?php }?>
        </tbody>

    </table>

*/
class CSVReader {
    
    var $fields;        /** columns names retrieved after parsing */
    var $separator = ';';    /** separator used to explode each line */
    
    /**
     * Parse a text containing CSV formatted data.
     *
     * @access    public
     * @param    string
     * @return    array
     */
    function parse_text($p_Text) {
        $lines = explode(";\n", $p_Text);
        return $this->parse_lines($lines);
    }
    
    /**
     * Parse a file containing CSV formatted data.
     *
     * @access    public
     * @param    string
     * @return    array
     */
    function parse_file($p_Filepath) {
        $lines = file($p_Filepath);
        return $this->parse_lines($lines);
    }
    /**
     * Parse an array of text lines containing CSV formatted data.
     *
     * @access    public
     * @param    array
     * @return    array
     */
    function parse_lines($p_CSVLines) {    
        $content = FALSE;
        foreach( $p_CSVLines as $line_num => $line ) {
            if( $line != '' ) { // skip empty lines
                //$elements = split($this->separator, $line);
                $elements = explode($this->separator, $line);
                if( !is_array($content) ) { // the first line contains fields names
                    $this->fields = $elements;
                    $content = array();
                } else {
                    $item = array();
                    foreach( $this->fields as $id => $field ) {
                     // printf("%d | ",$id);
                        if( isset($elements[$id]) && $id != 4  ) {
                            $item[$field] = trim($elements[$id]);
                        }
                    }
                    $content[] = $item;
                }
            }
        }
        return $content;
    }
}